<?php

namespace App\Models;


use App\Service\Base\Model;
use Illuminate\Support\Facades\DB;

class UserPay extends Model
{

    // 用户管理
    protected $table = 'user_pay';

    // 缓存字段，用于搜索绑定的字段
    protected $cacheFields = ['id', 'uid', 'auth_id', 'type', 'status'];

    //  {"id":"自编号","uid":"用户ID","auth_id":"认证ID","money":"金额","description":"描述","type":"操作类型：auth：认证 order：订单","status":"支付状态 0-未支付 1-支付成功 2-支付失败","ctime":"注册时间","mtime":"更新时间"}


    // array ( 'id' => '$this->getData("id", null, "trim")', 'uid' => '$this->getData("uid", null, "trim")', 'auth_id' => '$this->getData("auth_id", null, "trim")', 'money' => '$this->getData("money", null, "trim")', 'description' => '$this->getData("description", null, "trim")', 'type' => '$this->getData("type", null, "trim")', 'status' => '$this->getData("status", null, "trim")', 'ctime' => '$this->getData("ctime", null, "trim")', 'mtime' => '$this->getData("mtime", null, "trim")', )


    // $id = $this->getData("id", null, 'trim'); $uid = $this->getData("uid", null, 'trim'); $auth_id = $this->getData("auth_id", null, 'trim'); $money = $this->getData("money", null, 'trim'); $description = $this->getData("description", null, 'trim'); $type = $this->getData("type", null, 'trim'); $status = $this->getData("status", null, 'trim'); $ctime = $this->getData("ctime", null, 'trim'); $mtime = $this->getData("mtime", null, 'trim');

    // 支付状态
    public function getSatus($getKey = true)
    {
        $channels = [
            // 其他  暂时没有用的
            '0' => '未支付',
            '1' => '支付成功',
            '2' => '支付失败',
        ];

        if(empty($getKey)){
            return $channels;
        }

        return array_keys($channels);
    }

    // 支付类型
    public function getType($getKey = true)
    {
        $channels = [
            'auth'  => '认证',
            'order' => '订单',
        ];

        if(empty($getKey)){
            return $channels;
        }

        return array_keys($channels);
    }


    // 进行支付：完成订单报名
    // 进行支付：完成认证
    public function toPay($trade_no)
    {
        // 查找支付记录
        $info = $this->where('trade_no', $trade_no)->first();
        if(empty($info)){
            return $this->return_false('订单不存在！');
        }
        $info  = $info->toArray();
        $uid   = $info['uid'];
        $money = $info['money'];
        if($info['status'] == 1){
            return $this->return_false('订单已经支付！');
        }
        // 查找推荐人
        $user  = (new User())->isCache(true)->oneFind(['id' => $uid]);
        $p_uid = $user['from_uid'];

        // 查找配置的代理提成金额
        $config  = (new SiteConfig())->getConfig();
        $is_open = $config['open_agent'] ?? 0;
        $_rate   = $config['agent_rate'] ?? 0;
        $_addMon = $money * $_rate / 100;


        DB::beginTransaction();
        try{
            // 支付操作：第1步：完成支付记录
            $this->toPayForPay($info['id']);

            // 完成认证：
            // 余额：
            // 保存信息

            $result = $userDb->where(['id' => $info['uid']])->update(['end_time' => $nextYear]);

            $result = $authDb->where(['id' => $auth_id])->update($auth);


            DB::commit();
        } catch(\Exception $e){
            DB::rollBack();

            return $this->return_false($e->getMessage());
        }

        $auth['id'] = $auth_id;
        $userDb->deleteCacheShow(['id' => $info['uid']]);
        $this->deleteCacheShow($info, $auth);

        return true;
    }

    // 添加支付记录：oneAdd
    //   认证：
    //   报名订单：

    // 完成认证 用户：结束时间 end_time  认证：结束时间 form_time end_time is_pay：1
    public function finishAuth($auth_id)
    {
        // 明年的明天一早结束时间戳
        $nextYear = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y") + 1);  //这里+1

        // 判断是否已经认证
        $userDb = new User();
        $authDb = new UserAuth();
        $auth   = ['form_time' => LARAVEL_TIME, 'end_time' => $nextYear, 'is_pay' => 1,];

        // 查找认证信息
        $info = $authDb->isCache(true)->oneFind(['id' => $auth_id]);
        if(empty($info)){
            return $this->return_false('认证信息不存在！');
        }

        // 保存信息
        DB::beginTransaction();
        try{
            // 改变用户信息
            $result = $userDb->where(['id' => $info['uid']])->update(['end_time' => $nextYear]);

            $result = $authDb->where(['id' => $auth_id])->update($auth);


            DB::commit();
        } catch(\Exception $e){
            DB::rollBack();

            return $this->return_false($e->getMessage());
        }

        $auth['id'] = $auth_id;
        $userDb->deleteCacheShow(['id' => $info['uid']]);
        $this->deleteCacheShow($info, $auth);

        return true;
    }

    // 支付操作：第1步：完成支付记录
    public function toPayForPay($id)
    {
        $this->where('id', $id)->update(['status' => 1]);
    }

    // 支付操作：第2步：收益
    public function toPayForProfit($p_uid, $money)
    {
        // 增加用户余额
        (new User())->where('id', $p_uid)->update(['money' => DB::raw('money + '.$money)]);
        // 增加用户余额明细

        // 增加用户收益明细

    }

    // 支付操作：第3步：完成认证
    public function toPayForFinishAuth() { }

    // 支付操作：第3步：用户余额
    public function toPayForMoney() { }

    // 支付操作：第3步：完成订单
    public function toPayForOrder() { }

    // 定义验证操作
    public function rules()
    {

        $add = [
        ];

        $edit = [
        ];

        $info = [
        ];


        $rule = [
            'uid'         => 'required',
            'auth_id'     => 'required',
            'money'       => 'required',
            'description' => 'required',
            'type'        => 'required',
            'status'      => 'in:0,1,2',
        ];


        $msg = [
            'id'          => '自编号',
            'uid'         => '用户ID',
            'auth_id'     => '认证ID',
            'money'       => '金额',
            'description' => '描述',
            'type'        => '操作类型：auth：认证 order：订单',
            'status'      => '支付状态 0-未支付 1-支付成功 2-支付失败',
            'ctime'       => '注册时间',
            'mtime'       => '更新时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add];
    }
}
